package edu.ynu.onlinesqltool.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ynu.onlinesqltool.dao.SQLExcution;

public class QueryAction implements IAction {

	public void execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String sql = req.getParameter("sql");
		int pageNo = Integer.parseInt(req.getParameter("pageNo"));
		try {
			List records = new ArrayList();
			int total = SQLExcution.quey(records,sql,pageNo,5);
			req.setAttribute("records", records);
			RequestDispatcher dispater=req.getRequestDispatcher(resp.encodeURL("/result.jsp?sql="+sql+"&pageNo="+pageNo+"&total="+total));
			dispater.forward(req,resp);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			req.setAttribute("error", e);
			RequestDispatcher dispater=req.getRequestDispatcher(resp.encodeURL("/error.jsp"));
			dispater.forward(req,resp);
		}

	}

}
